package com.example.dragandview.notice;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.dragandview.R;


public class AlertDialogaActivity extends Activity {
    private Button btn_alert_bt1 , btn_alert_bt2 ,btn_alert_bt3,btn_alert_bt4,btn_alert_bt5;
    private String[] strings = new String[]{"item1","item2","item3","item4"};
    private CharSequence  strings1[] = new String[]{"item1","item2","item3","item4"};
    private final boolean[] booleans = new boolean[]{false,false,false,false};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notice_alertdialog);
        btn_alert_bt1 = findViewById(R.id.btn_alert_bt1);
        btn_alert_bt2 = findViewById(R.id.btn_alert_bt2);
        btn_alert_bt3 = findViewById(R.id.btn_alert_bt3);
        btn_alert_bt4 = findViewById(R.id.btn_alert_bt4);
        btn_alert_bt5 = findViewById(R.id.btn_alert_bt5);
        btn_alert_bt1.setOnClickListener(v -> {
            AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogaActivity.this).create();
            //设置图标
            alertDialog.setIcon(R.drawable.meinv1);
            //设置标题
            alertDialog.setTitle("我是标题");
            //设置要显示的内容
            alertDialog.setMessage("To be or not to be ? ");
            //添加取消按钮
            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(AlertDialogaActivity.this,"我点击了否按钮",Toast.LENGTH_SHORT).show();
                }
            });
            //添加确定按钮
            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(AlertDialogaActivity.this,"我点击了 是 按钮",Toast.LENGTH_SHORT).show();
                }
            });
            alertDialog.show();
        });
        btn_alert_bt2.setOnClickListener(v -> {

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogaActivity.this);
            //设置图标
            alertDialog.setIcon(R.drawable.meinv1);
            //设置标题
            alertDialog.setTitle("我是标题");
            //添加列表内容
            alertDialog.setItems(strings, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(AlertDialogaActivity.this,"我点击了"+strings[which],Toast.LENGTH_SHORT).show();
                }
            });
            alertDialog.create().show();
        });
        btn_alert_bt3.setOnClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogaActivity.this);
            //设置图标
            builder.setIcon(R.drawable.meinv1);
            //设置标题
            builder.setTitle("我是标题");
            //设置列表项
            builder.setSingleChoiceItems(strings, 0, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(AlertDialogaActivity.this,"我点击了"+strings[which],Toast.LENGTH_SHORT).show();
                }
            });
            builder.setPositiveButton("确定",null);
            builder.create().show();
        });
        btn_alert_bt4.setOnClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogaActivity.this);
            //设置图标
            builder.setIcon(R.drawable.meinv1);
            //设置标题
            builder.setTitle("我是标题");
            //设置列表项
            builder.setMultiChoiceItems(strings1, booleans, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    booleans[which] = isChecked;
                }
            });

            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String res = "";
                    for (int i = 0 ; i < booleans.length ; i++){
                        if(booleans[i]){
                            res += strings1[i]+"、";
                        }
                    }
                    if(res.equals("")){
                        Toast.makeText(AlertDialogaActivity.this,"我什么都没选择",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(AlertDialogaActivity.this,"我选择了"+res,Toast.LENGTH_SHORT).show();
                    }
                }
            });
            builder.setNegativeButton("取消",null);
            builder.create().show();
        });
        btn_alert_bt5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiyDialog1 diyDialog1 = new DiyDialog1(AlertDialogaActivity.this);
                diyDialog1.show();
            }
        });
    }
}
